/***********************************************************************************************************************************
 * Name:	Configure_FQDN.sql
 * Author:	Frank Figearo (frank.figearo@us.bbaaviation.com|frank@sqlnerd.me)
 * Summary:	Set the @@SERVERNAME to the fully qualified domain name.
 */
USE master;
GO
DECLARE
	@Domain			NVARCHAR(128)	= N'.BBAAviation.net',
	@Old_Name		NVARCHAR(128)	= (SELECT name FROM sys.servers WHERE server_id = 0),
	@NetBIOS_Name	NVARCHAR(128)	= CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS NVARCHAR(128)),
	@Instance_Name	NVARCHAR(128)	= CAST(SERVERPROPERTY('InstanceName')  AS NVARCHAR(128)),
	@FQDN			NVARCHAR(128);

IF @Instance_Name IS NULL SET @Instance_Name= N'' ELSE SET @Instance_Name= N'\' + @Instance_Name;
SET @FQDN= CAST(SERVERPROPERTY('MachineName') AS NVARCHAR(128)) + @domain + @Instance_Name;

SELECT OldName= @Old_Name, FQDN= @FQDN, NetBIOS_Name= @NetBIOS_Name;

EXECUTE sp_dropserver @Old_Name;
EXECUTE sp_addserver @FQDN, local;
EXECUTE sp_setnetname @server= @FQDN, @netname= @NetBIOS_Name;
GO